
//
// 110410 - AH - (Fantoft Club) Created, first synesthesic component, it is 
//               Greek and cool.
// 110418 - AH - (Scotchman) set(URL).
// 110603 - AH - Change because ImageUtil.scale used no longer throw IOException
//               and missing java doc.
//

package org.soekrat.gui.swing.synesthesia;

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.soekrat.AttributeObject;
import org.soekrat.gui.swing.TestFrame;

/**
 * <p>
 *   Panel to show image.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class ImagePanel extends JPanel 
{
  private BufferedImage m_image=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  public ImagePanel()
  {
    setLayout(new BorderLayout());
  }
  
  /**
   * <p>
   *   Sets
   *   {@link AttributeObject} to show image for.
   * </p>
   * @param ao {@link AttributeObject} that has attribute named {@code image}
   *           which value is assumed to be text representation of image's
   *           URL.
   */
  public void set(AttributeObject ao)
  {
    if (ao==null)
    {
      throw new NullPointerException("ao");
    }
    
    try
    {
      String imageUrl=ao.getString("image");
      if (imageUrl!=null)
      {
        URL url=new URL(imageUrl);
        //m_image=ImageIO.read(url);
        set(url);
      }  
    }
    catch (IOException iox)
    {
      m_image=null;
      repaint();
    }
  }
  
  /**
   * <p>
   *   Sets image to show.
   * </p>
   * @param url {@link URL} to image.
   */
  public void set(URL url)
  {
    if (url==null)
    {
      throw new NullPointerException("url");
    }
    
    //Image image=ImageIO.read(url);
      
    int w=getWidth();
    int h=getHeight();
      
    if (w<h)
    {
      m_image=ImageUtil.scale(url, w, -1, this);
    }
    else
    {
      m_image=ImageUtil.scale(url, -1, h, this);
    }
      
    repaint();  
  }

  @Override
  protected void paintComponent(Graphics g)
  {
    //int w=getWidth();
    //int h=getHeight();
    
  
    if (m_image!=null) 
    {
      int iw=m_image.getWidth();
      int ih=m_image.getHeight();
      g.drawImage(m_image, 0, 0, iw, ih, this);
    }
    else
    {
      // TODO, handle we got no image.
    }
  }
  
  /**
   * <p>
   *   Test program.
   * </p>
   * @param args Command line arguments, not used.
   */
  public static void main(String[] args) 
  {
    AttributeObject ao=new AttributeObject();
    ao.setAttribute("image", 
      "http://farm6.static.flickr.com/5105/5603586551_9aca3c5055_m.jpg");
    
    JFrame f=new JFrame();
    f.setLayout(new BorderLayout());
    
    ImagePanel ip=new ImagePanel();
    f.add(ip, BorderLayout.CENTER);
 
    ip.set(ao);
    
    f.pack();
    f.setSize(300, 300);
    f.setVisible(true);
  }
  
  /**
   * <p>
   *   Shows image in a 
   *   {@link JFrame}.
   * </p>
   * @param url {@link URL} to image.
   */
  public static void showInFrame(URL url)
  {
    ImagePanel ip=new ImagePanel();
    ip.set(url);
    TestFrame.show(ip,500, 500);
  }

}
